//package com.mediacloud.app.newsmodule.fragment
//
//import android.os.Build
//import android.os.Bundle
//import android.text.TextUtils
//import android.util.Log
//import android.view.View
//import android.view.ViewGroup
//import androidx.annotation.RequiresApi
//import androidx.recyclerview.widget.GridLayoutManager
//import com.alibaba.fastjson.JSON
//import com.mediacloud.app.cloud.ijkplayersdk.video.MultiVideoMannager
//import com.mediacloud.app.fbnavsk.navPaddingTopMatch
//import com.mediacloud.app.model.news.ArticleItem
//import com.mediacloud.app.model.news.ArticleListData
//import com.mediacloud.app.model.news.CatalogItem
//import com.mediacloud.app.nav2.fragment.HqyNavFragment
//import com.mediacloud.app.newsmodule.R
//import com.mediacloud.app.newsmodule.adaptor.component.ComponentListItemStyleAdaptor
//import com.mediacloud.app.newsmodule.fragment.video.vod.PlayClickListener
//import com.mediacloud.app.newsmodule.fragment.video.vod.VideoListPlayerUtil
//import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
//import com.mediacloud.app.reslib.util.DataInvokeUtil
//import com.scwang.smartrefresh.layout.api.RefreshLayout
//import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
//import io.reactivex.Observable
//import io.reactivex.Observer
//import io.reactivex.android.schedulers.AndroidSchedulers
//import io.reactivex.disposables.Disposable
//import io.reactivex.schedulers.Schedulers
//import kotlinx.android.synthetic.main.fragment_info_flow.*
//
//class InfoFlowFragmentB : HqyNavFragment(), OnRefreshLoadMoreListener, PlayClickListener {
//    private var mVideoListPlayerUtil: VideoListPlayerUtil? = null
//    var smallSize = 4
//    var multiple = 4 //倍数
//    var catalogID: String? = null
//
//    private var catalogItem: CatalogItem? = null
//
//    private var pageNum = 1
//    private var pageSize = 20
//    private var adapter: InfoFlowAdapterB? = null
//    private var second_catalog_id = ""
//    private var second_model = ""
//    private var hasMore = false
//    override fun getLayoutResID(): Int {
//        return R.layout.fragment_info_flow
//    }
//
//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//
//        navPaddingTopMatch()
//        if (fragmentArguments != null) {
//            saveInstanceArguments = fragmentArguments
//            catalogID = saveInstanceArguments?.getString("id")
//            catalogItem = saveInstanceArguments?.getParcelable("catalog")
//        } else if (saveInstanceArguments != null) {
//            catalogID = saveInstanceArguments!!.getString("id")
//            catalogItem = saveInstanceArguments!!.getParcelable("catalog")
//        }
//
//        adapter = InfoFlowAdapterB(requireContext(), catalogItem)
//        refreshLayout.setOnRefreshLoadMoreListener(this)
//        recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
//        recyclerView.adapter = adapter
//        adapter?.mPlayClickListener = this
//        recyclerView.setNestedScrollingEnabled(false);
//        recyclerView.setHasFixedSize(false);
//        recyclerView.setFocusable(false);
//        getConfig()
//
//        mVideoListPlayerUtil = VideoListPlayerUtil(activity, recyclerView)
//        mVideoListPlayerUtil?.currentFragment=this
//        mVideoListPlayerUtil?.isAutoPlayVideo=true
//    }
//
//    override fun initView() {
//        loadingView = findViewById(R.id.mLoadingView);
//        initStateView();
//        loadingImage.setOnClickListener {
//            if (currentState != TYPE_LOADING) {
//                showStateView(TYPE_LOADING, false)
//                onRefresh(refreshLayout)
//            }
//        }
//        showStateView(TYPE_LOADING, false)
//        refreshLayout.setOnRefreshLoadMoreListener(this)
//
//    }
//
//    override fun onRefresh(refreshLayout: RefreshLayout) {
//        pageNum = 1
//        getDate()
//    }
//
//    override fun onLoadMore(refreshLayout: RefreshLayout) {
//        pageNum++
//        getDate()
//    }
//
//
//    private fun getConfig() {
//        val d = DataInvokeUtil.getContentListResult(catalogID,1,1,null).subscribe {
//                it.body().optJSONObject("data")?.optJSONObject("navigate")?.let {
//                    second_catalog_id = it.optString("second_catalog_id")
//                    second_model = it.optString("second_model")
//                    smallSize = it.optInt("staggered_page")
//                    pageSize = smallSize * multiple
//                }
//                getDate()
//            }
//    }
//
//    private fun getDate() {
//        val observable1 = DataInvokeUtil.getContentList(catalogID, pageNum,pageSize,null)
//        val observable2 = DataInvokeUtil.ziMeiTiApi.getCMSList(
//            second_catalog_id,
//            second_model,
//            pageNum,
//            multiple,
//            "android"
//        )
//        val d = Observable.zip(
//            observable1,
//            observable2
//        ) { t1, t2 ->
//            val item = InfoFlowItem()
//            val smallList: MutableList<ArticleItem> = ArrayList()
//            if (t1.isSuccessful) {
//                val temp = ArticleListData()
//                temp.readFromJson(t1.body())
//                item.componentItems = temp.componentItems
//                smallList.addAll(temp.articleList)
//                hasMore = temp.more
//            }
//            val bigList: MutableList<ArticleItem> = ArrayList()
//            if (t2.isSuccessful) {
//                val state = t2.body().optBoolean("state")
//                if (state) {
//                    val data = t2.body()?.optJSONObject("data")
//                    data?.optJSONArray("meta")?.let {
//                        val size = it.length()
//                        for (i in 0 until size) {
//                            val articleItem =
//                                JSON.parseObject("" + it.optJSONObject(i), ArticleItem::class.java)
//                            articleItem.infoFlowBig = 1
//                            bigList.add(articleItem)
//                        }
//                    }
//                    val lastPage = data?.optJSONObject("paging")?.optInt("lastPage") ?: 0
//                    hasMore = hasMore || pageNum < lastPage
//                }
//            }
//
//
//            if (smallList.isNotEmpty() && bigList.isNotEmpty()) {
//                val size = smallList.size
//                for (i in 0 until size) {
//                    if (i % smallSize == 0 && bigList.isNotEmpty()) {
//                        item.articleList.add(bigList.removeAt(0))
//                    }
//                    item.articleList.add(smallList[i])
//                }
//                item.articleList.addAll(bigList)
//            } else if (smallList.isEmpty()) {
//                //没小图了就添加所有大图
//                item.articleList.addAll(bigList)
//            } else if (bigList.isEmpty()) {
//                //同理
//                item.articleList.addAll(smallList)
//            }
//            item
//        }.subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(object : Observer<InfoFlowItem> {
//                override fun onSubscribe(d: Disposable) {
//
//                }
//
//                @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
//                override fun onNext(t: InfoFlowItem) {
//                    refreshLayout.finishRefresh()
//                    refreshLayout.finishLoadMore()
//                    if (pageNum == 1) {
//                        adapter?.articleItems?.clear()
//                        t.componentItems?.let {
//                            if (it.isNotEmpty()) {
//                                adapter?.articleItems?.add(t.componentItems)
//                            }
//                        }
//                        adapter?.articleItems?.addAll(t.articleList)
//                        adapter?.reload = true
//                        adapter?.notifyDataSetChanged()
//                    } else {
//                        adapter?.addItems(t.articleList)
//                    }
//                    closeStateView()
//                }
//
//                override fun onError(e: Throwable) {
//                    refreshLayout.finishRefresh()
//                    refreshLayout.finishLoadMore()
//                    closeStateView()
//                }
//
//                override fun onComplete() {
//
//                }
//            })
//    }
//
//    override fun onPlayIcoClick(rootView: View, container: ViewGroup?, item: ArticleItem) {
//        if (mVideoListPlayerUtil == null) {
//            mVideoListPlayerUtil = VideoListPlayerUtil(activity, recyclerView)
//            mVideoListPlayerUtil?.currentFragment=this
//        }
//        mVideoListPlayerUtil?.setPlayerContainer(
//            rootView, container, !TextUtils.isEmpty(item.goodsID), {
//                item.parentId = catalogID
//                NewsItemClickUtils.OpenItemNewsHandle(activity, item.getType(), item, catalogItem)
//            }, item
//        )
//        mVideoListPlayerUtil?.setVideoDetail("$catalogID", item)
//    }
//
//    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
//        super.setUserVisibleHint(isVisibleToUser)
//        if (userVisibleHint) {
//        } else {
//            mVideoListPlayerUtil?.stopPlay()
//        }
//    }
//
//    override fun onHiddenChanged(hidden: Boolean) {
//        super.onHiddenChanged(hidden)
//        if (hidden) {
//            mVideoListPlayerUtil?.stopPlay()
//        }
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        mVideoListPlayerUtil?.onDestroy()
//    }
//}